package com.lhb.boyixue.user.controller;



import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author 86131
 * @version 1.0
 * @description pdf下载接口
 * @date 2025/5/24 下午11:27
 */
@RestController
@RequestMapping("/pdf")
public class PdfController {

    // 使用类加载器加载资源
    private final PathMatchingResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();

    @GetMapping("/download")
    public ResponseEntity<InputStreamResource> downloadPdfPackage() throws IOException {

        // 创建一个字节数组输出流，用于存储ZIP文件
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ZipOutputStream zos = new ZipOutputStream(baos);

        try {

             {
                 // 构建资源路径 (使用classpath:前缀)
                 String resourcePath = "classpath:pdf/"  + "*.pdf";
                 // 获取匹配的所有PDF资源
                 Resource[] resources = resourceResolver.getResources(resourcePath);
                 for (Resource resource : resources) {
                     addFileToZip(zos, resource);
                 }
            }
        } finally {
            zos.close();
        }

        // 准备HTTP响应
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

        // 使用UTF-8编码文件名
        String fileName = "exam.zip";
        String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString())
                .replace("+", "%20");

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION,
                "attachment; filename=\"" + fileName + "\"; filename*=UTF-8''" + encodedFileName);

        return ResponseEntity
                .ok()
                .headers(headers)
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(new InputStreamResource(bais));
    }


    private void addFileToZip(ZipOutputStream zos, Resource resource) throws IOException {
        byte[] buffer = new byte[1024];
        InputStream fis = resource.getInputStream();

        // 创建ZIP条目
        zos.putNextEntry(new ZipEntry(resource.getFilename()));

        int length;
        // 将文件内容写入ZIP流
        while ((length = fis.read(buffer)) > 0) {
            zos.write(buffer, 0, length);
        }

        zos.closeEntry();
        fis.close();
    }
}
